package com.maya.medicalwastes.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.maya.medicalwastes.ParamConstant;
import com.maya.medicalwastes.R;
import com.maya.medicalwastes.adapter.TadayCollectAdapter;
import com.maya.medicalwastes.adapter.WareItemAdapter;
import com.maya.medicalwastes.entity.CollectBean;
import com.maya.medicalwastes.entity.KeyBean;
import com.maya.medicalwastes.entity.WaitWareBean;
import com.maya.medicalwastes.httpNet.ApiService;
import com.maya.medicalwastes.httpNet.HttpService;
import com.maya.medicalwastes.httpNet.callback.ResponseCallBack;
import com.maya.medicalwastes.httpNet.common.SharedPreferencesConstant;
import com.maya.medicalwastes.httpNet.manage.RetrofitManager;
import com.maya.medicalwastes.httpNet.model.ResultModel;
import com.maya.medicalwastes.util.DateUtils;
import com.maya.medicalwastes.util.FormatUtilA;
import com.maya.medicalwastes.util.PrefUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author created by fuguoyong
 * 版本：1.0
 * 创建日期：2020-11-19 15
 * 描述：CollectActivity
 */
public class TodayCollectActivity extends AppCompatActivity {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.tv_carSameDayNum)
    TextView tv_carSameDayNum;
    @BindView(R.id.tv_carSameDayWeight)
    TextView tv_carSameDayWeight;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.type)
    RecyclerView typeRecy;
    List<CollectBean> mlist = new ArrayList<>();
    List<KeyBean> keyBeans = new ArrayList<>();
    TadayCollectAdapter collectAdapter;
    WareItemAdapter wareItemAdapter;
private int page=1;
private String key="";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_collect);
        ButterKnife.bind(this);
        tv_title.setText("今日收集");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        collectAdapter = new TadayCollectAdapter(R.layout.list_taday_collect_item, mlist);
        recyclerView.setAdapter(collectAdapter);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.HORIZONTAL);
        typeRecy.setLayoutManager(manager);
        wareItemAdapter = new WareItemAdapter(R.layout.list_waste_type_item, keyBeans);
        typeRecy.setAdapter(wareItemAdapter);
        wareItemAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_type) {
                    for (KeyBean bean : keyBeans) {
                        bean.setCheck(false);
                    }
                    keyBeans.get(position).setCheck(true);
                    wareItemAdapter.notifyDataSetChanged();
                    key=wareItemAdapter.getItem(position).getKey();
                    page=1;
                    queryByPage(key,false);
                }
            }
        });
        keySearch();
        queryByPage("",false);

        collectAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                queryByPage(key,true);
            }
        });


    }

    private void queryByPage(String key,boolean flag) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum", page + "");
        param.put("pageSize", "10");
        param.put("collectCarId", "200");
        param.put("createTime", "2020-12-08");
        param.put("garbageCategory", key);
        Observable<ResultModel<WaitWareBean>> call = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).queryByPage(param, PrefUtils.getString(this, SharedPreferencesConstant.TOKEN, ""));
        HttpService.getInstance().enqueue(call, new ResponseCallBack<ResultModel<WaitWareBean>>() {
            @Override
            public void onResponse(ResultModel<WaitWareBean> response) {
                if (response.httpCode == 200) {
                    if (response.data.getPageInfo().getList().size()>0){
                        if (flag){
                            collectAdapter.addData(response.data.getPageInfo().getList());
                            collectAdapter.loadMoreComplete();
                        }else {
                            collectAdapter.replaceData(response.data.getPageInfo().getList());
                        }
                    } else{
                        collectAdapter.loadMoreEnd();
                    }
                    tv_carSameDayNum.setText(response.data.getSameDayTotal().getNum() + "件");
                    tv_carSameDayWeight.setText(FormatUtilA.numbFormat(Double.valueOf(response.data.getSameDayTotal().getSumWeight())) + "kg");
                }

            }
            @Override
            public void onFailure(int httpCode) {
            }
        }, this);


    }


    public void keySearch() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("key", "CATEGORY");
        param.put(ParamConstant.TOKEN, PrefUtils.getString(this, SharedPreferencesConstant.TOKEN, ""));
        Observable<ResultModel<ArrayList<KeyBean>>> call = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).Keyword(param);
        HttpService.getInstance().enqueue(call, new ResponseCallBack<ResultModel<ArrayList<KeyBean>>>() {
            @Override
            public void onResponse(ResultModel<ArrayList<KeyBean>> response) {
                if (response != null && response.httpCode == 200) {
                    response.data.add(0, new KeyBean("", "全部", true));
//                    keyBeans.addAll(response.data);
                    wareItemAdapter.replaceData(response.data);
                }
            }

            @Override
            public void onFailure(int httpCode) {
            }
        }, this);


    }


}
